import Vue from 'vue'
import Router from 'vue-router'
import routes from './routes/index'
import store from '../store'
import globalService from "@/services/globalService.js";


const whiteList = ['/login']

Vue.use(Router)

const originPush = Router.prototype.push
Router.prototype.push = function(info) {
  return originPush.call(this, info).catch((err) => err)
}
const originReplace = Router.prototype.replace
Router.prototype.replace = function(info) {
  return originReplace.call(this, info).catch((err) => err)
}
const createRouter = () =>
  new Router({
    mode: 'hash',
    scrollBehavior: () => ({ y: 0 }),
    routes
  })

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}
const getnewFullPath = obj => {
  const query = obj.query
  delete query.token
  let str = ''
  Object.keys(query).forEach(key => {
    str = str + `${key}=${query[key]}&`
  })
  let path = str ? obj.path + '?' + str : obj.path
  return path
}
router.beforeEach(async (to, from, next) => {
  // if (to.query.token) {
  //   let res = await globalService.getRealToken({
  //     token: to.query.token,
  //   });
  //   // 临时token过期返回app
  //   if (res.code == '400') {
  //     if (to.query.originFrom) {
  //       if (to.query.originFrom === 'app_ios') {
  //         window.webkit.messageHandlers.common.postMessage(JSON.stringify({ operation: '返回' }));
  //       } else if (to.query.originFrom === 'app_android') {
  //         window.android.finishActivity();
  //       } else if (to.query.originFrom === 'app_android_new') {
  //         window.ReactNativeWebView.postMessage(JSON.stringify({
  //           method: 'goBack',
  //           params: {},
  //         })); 
  //       }
  //     }
  //   }
  //   if (res.data) {
  //     store.commit('user/SET_TOKEN', res.data);
  //     localStorage.setItem('tokenId', res.data)
  //     if (to.query.originFrom) {
  //       localStorage.setItem('originFrom', to.query.originFrom);
  //     }
  //   }
  //   next({ path: getnewFullPath(to) })
  //   if (localStorage.getItem('tokenId')) {
  //     document.title = to.meta.title
  //     if (whiteList.indexOf(to.path) === -1) {
  //       await store.dispatch('user/getAccountInfo');
  //       await store.dispatch('user/getMenuList');
  //     }
  //   }
  // } else {
  //   if (localStorage.getItem('tokenId')) {
  //     document.title = to.meta.title
  //     if (whiteList.indexOf(to.path) === -1) {
  //       await store.dispatch('user/getAccountInfo');
  //       await store.dispatch('user/getMenuList');
  //     }
  //   }
  //   if (whiteList.indexOf(to.path) !== -1) {
  //     next()
  //   } else {
  //     try {
  //       if (!window.localStorage.getItem('tokenId')) {
  //         next(`/login`)
  //         return
  //       }
  //       next()
  //     } catch (error) {
  //       next(`/login`)
  //     }
  //   }
  // }

      // const userInfo = {
      //   id: '23040300234',
      //   token: '3400f8e32eae4a698827dba7392f6567',
      //   version: '1.1.0',
      //   tenantId: '5',
      //   permissionLists:'',
      //   tenantName:'xxxxx',
      //   customerId:'384535466983173',
      //   uuUserId:'384535466983174'
      // }

      // localStorage.setItem('userInfo', JSON.stringify(userInfo))
      // localStorage.setItem('token', userInfo.token)
      // localStorage.setItem('tenantId', userInfo.tenantId)

if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      try {
        if (!window.localStorage.getItem('token')) {
          next(`/login`)
          return
        }
        next()
      } catch (error) {
        next(`/login`)
      }
    }
})

export default router
